﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using BLL;
using CrystalDecisions.CrystalReports.Engine;
using Model;

public partial class ViewbyProduct_ZTEElectronicsList_ZTEElectronicsList : System.Web.UI.Page
{
    private Test t;
    private TestItem ti;
    private ZTEPIMAndPower zte;

    public ViewbyProduct_ZTEElectronicsList_ZTEElectronicsList()
    {
        t = new Test();
        ti = new TestItem();
        zte = new ZTEPIMAndPower();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Page.Unload += new EventHandler(Page_Unload);
        }
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string SN = this.TextBox1.Text.Trim();
        if (this.DropDownList1.SelectedValue == "按成品序列号查询")
        {
            PackageProduct pp = new PackageProduct();
            SN = pp.GetPackageProductbyPackageProductSN(SN).ProductSN;
            if (string.IsNullOrEmpty(SN))
            {
                string str1 = "未关联的成品序列号！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
        }

        TestInfo test1 = GetTestData(SN);
        TestInfo test2 = GetHighFTestData(SN);
        IList<TestItemInfo> list1 = GetTestItemData(SN);
        IList<TestItemInfo> list2 = GetHighFTestItemData(SN);
        IList<ZTEPIMAndPowerInfo> list3 = GetPIMData(SN);
        IList<ZTEPIMAndPowerInfo> list4 = GetPowerData(SN);
        BindCR(test1,test2, list1, list2, list3, list4);


        //ParameterFields paramFields = new ParameterFields();

        ////设置传递第一个参数
        //ParameterField paramField = new ParameterField();
        //ParameterDiscreteValue discreteVal = new ParameterDiscreteValue();
        //paramField.ParameterFieldName = "ProductSN";
        //discreteVal.Value = SN;
        //paramField.CurrentValues.Add(discreteVal);
        //paramFields.Add(paramField);

        //// 定义ReportDocument对象，装载ZTEElectronicsList.rpt
        //ReportDocument rd = new ReportDocument();
        //rd.Load(Server.MapPath("ZTEElectronicsList.rpt"));

        ////定义水晶报表的数据库连接信息
        //rd.Subreports[0].SetDatabaseLogon("sa", "mobi@20120510", "10.53.6.170", "ATE");
        //rd.Subreports[1].SetDatabaseLogon("sa", "mobi@20120510", "10.53.6.170", "ATE");
        //rd.Subreports[2].SetDatabaseLogon("sa", "mobi@20120510", "10.53.6.170", "ATE");
        //rd.Subreports[3].SetDatabaseLogon("sa", "mobi@20120510", "10.53.6.170", "ATE");

        ////ConnectionInfo connectionInfo = new ConnectionInfo();
        ////connectionInfo.DatabaseName = "ATE";
        ////connectionInfo.UserID = "sa";
        ////connectionInfo.Password = "mobi@20120510";
        ////connectionInfo.ServerName = "10.53.6.170";

        //////将数据库信息传递给报表
        ////SetDBLogonForReport(connectionInfo, rd);

        ////把模板对象赋给报表前端呈现控件CrystalReportViewer1
        //CrystalReportViewer1.ReportSource = rd;

        //this.CrystalReportViewer1.ParameterFieldInfo = paramFields;
        
        //this.CrystalReportViewer1.DataBind();
        ////this.CrystalReportViewer1 .RefreshReport ();

        //this.CrystalReportViewer1.Visible = true;
    }

    private void BindCR(TestInfo test1,TestInfo test2, IList<TestItemInfo> list1, IList<TestItemInfo> list2, IList<ZTEPIMAndPowerInfo> list3, IList<ZTEPIMAndPowerInfo> list4)
    {
        this.CrystalReportSource1.ReportDocument.Load(Server.MapPath("ZTEElectronicsList.rpt"));

        ReportDocument rd = this.CrystalReportSource1.ReportDocument;
        TextObject sTestText = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text1"];
        sTestText.Text = test1.ProductSN + "    " + test1.TestTime.ToString() + "    " + test1.ProductTypeID + "    " + test1.ENAID + "    " + test1.UserID + "    " + test1.IsPass;
        TextObject highFTestText = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text2"];
        highFTestText.Text = test2.ProductSN + "    " + test2.TestTime.ToString() + "    " + test2.ProductTypeID + "    " + test2.ENAID + "    " + test2.UserID + "    " + test2.IsPass; ;

        SubreportObject oSubreport1 = rd.ReportDefinition.ReportObjects["Subreport1"] as SubreportObject;
        ReportDocument oSub1 = oSubreport1.OpenSubreport("STestList.rpt");
        oSub1.SetDataSource(list1);
        SubreportObject oSubreport2 = rd.ReportDefinition.ReportObjects["Subreport2"] as SubreportObject;
        ReportDocument oSub2 = oSubreport2.OpenSubreport("HighFTestList.rpt");
        oSub2.SetDataSource(list2);
        SubreportObject oSubreport3 = rd.ReportDefinition.ReportObjects["Subreport3"] as SubreportObject;
        ReportDocument oSub3 = oSubreport3.OpenSubreport("PIMTestList.rpt");
        oSub3.SetDataSource(list3);
        SubreportObject oSubreport4 = rd.ReportDefinition.ReportObjects["Subreport4"] as SubreportObject;
        ReportDocument oSub4 = oSubreport4.OpenSubreport("PowerTestList.rpt");
        oSub4.SetDataSource(list4);
        //以下对应不正确
        //rd.Subreports[0].SetDataSource(list1);
        //rd.Subreports[1].SetDataSource(list2);
        //rd.Subreports[2].SetDataSource(list3);
        //rd.Subreports[3].SetDataSource(list4);
        this.CrystalReportSource1.DataBind();
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        this.CrystalReportViewer1.DataBind();
        this.CrystalReportViewer1.Visible = true;
    }

    private TestInfo GetTestData(string productSN)
    {
        TestInfo test = new TestInfo();
        test = t.GetTestbyLastSN(productSN);
        return test;
    }

    private TestInfo GetHighFTestData(string productSN)
    {
        TestInfo test = new TestInfo();
        test = t.GetHgihFTestbyLastSN(productSN);
        return test;
    }

    private IList<TestItemInfo> GetTestItemData(string productSN)
    {
        IList<TestItemInfo> list = new List<TestItemInfo>();
        list = ti.GetTestItembyLastSN(productSN);
        return list;
    }

    private IList<TestItemInfo> GetHighFTestItemData(string productSN)
    {
        IList<TestItemInfo> list = new List<TestItemInfo>();
        list = ti.GetHighFTestItembyLastSN(productSN);
        return list;
    }

    private IList<ZTEPIMAndPowerInfo> GetPIMData(string productSN)
    {
        IList<ZTEPIMAndPowerInfo> list = new List<ZTEPIMAndPowerInfo>();
        list = zte.GetZTEPIMbySN(productSN);
        return list;
    }

    private IList<ZTEPIMAndPowerInfo> GetPowerData(string productSN)
    {
        IList<ZTEPIMAndPowerInfo> list = new List<ZTEPIMAndPowerInfo>();
        list = zte.GetZTEPowerbySN(productSN);
        return list;
    }
}
